<?php

/**
 * Description of abeeCart
 *
 * @author maciekmp
 */
class AbeeCart extends WP_Widget {

    /** Variables to setup the widget. */
    var $woo_widget_cssclass;
    var $woo_widget_description;
    var $woo_widget_idbase;
    var $woo_widget_name;

    /** constructor */
    public function __construct() {

        /* Widget variable settings. */
        $this->woo_widget_cssclass = 'widget_shopping_cart';
        $this->woo_widget_description = __("Display the user's Shopping Cart in the sidebar.", 'woocommerce');
        $this->woo_widget_idbase = 'abee_shopping_cart';
        $this->woo_widget_name = 'Koszyk Abee';

        /* Widget settings. */
        $widget_ops = array('classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description);

        /* Create the widget. */
        parent::__construct('abeeshopping_cart', $this->woo_widget_name, $widget_ops);
    }

    /** @see WP_Widget */
    function widget($args, $instance) {
        global $woocommerce;

        if (is_cart() || is_checkout())
            return;

        extract($args);
        if (!empty($instance['title']))
            $title = $instance['title']; else
            $title = __('Cart', 'woocommerce');
        $title = apply_filters('widget_title', $title, $instance, $this->id_base);
        $hide_if_empty = isset($instance['hide_if_empty']) && $instance['hide_if_empty'] ? '1' : '0';

        echo $before_widget;
        if ($title)
            echo $before_title . $title . $after_title;

        echo '<ul class="cart_list product_list_widget ';
        if ($hide_if_empty)
            echo 'hide_cart_widget_if_empty';
        echo '">';

        if (sizeof($woocommerce->cart->get_cart()) > 0) {
            
            $all_quanity = 0;
            $all_quanity = $woocommerce->cart->cart_contents_count;
            
            if($all_quanity == 1)
                echo '<li><a href="' . $woocommerce->cart->get_cart_url() . '">W koszyku jest '.$all_quanity.' produkt</a></li>';
            elseif($all_quanity < 5)
                echo '<li><a href="' . $woocommerce->cart->get_cart_url() . '">W koszyku są '.$all_quanity.' produkty</a></li>';
            else
                echo '<li><a href="' . $woocommerce->cart->get_cart_url() . '">W koszyku jest '.$all_quanity.' produktów</a></li>';

        } else {
            echo '<li class="empty">' . __('No products in the cart.', 'woocommerce') . '</li>';
        }
        echo '</ul>';

        if (sizeof($woocommerce->cart->get_cart()) > 0) {

            echo '<p class="total"><strong>' . __('Subtotal', 'woocommerce') . ':</strong> ' . $woocommerce->cart->get_cart_subtotal() . '</p>';

            do_action('woocommerce_widget_shopping_cart_before_buttons');

            echo '<p class="buttons"><a href="' . $woocommerce->cart->get_cart_url() . '" class="button">' . __('View Cart &rarr;', 'woocommerce') . '</a> <a href="' . $woocommerce->cart->get_checkout_url() . '" class="button checkout">' . __('Checkout &rarr;', 'woocommerce') . '</a></p>';
        }

        echo $after_widget;

        if ($hide_if_empty && sizeof($woocommerce->cart->get_cart()) == 0) {
            $inline_js = "
				jQuery('.hide_cart_widget_if_empty').closest('.widget').hide();
				jQuery('body').bind('adding_to_cart', function(){
					jQuery(this).find('.hide_cart_widget_if_empty').closest('.widget').fadeIn();
				});
			";

            $woocommerce->add_inline_js($inline_js);
        }
    }

    /** @see WP_Widget->update */
    function update($new_instance, $old_instance) {
        $instance['title'] = strip_tags(stripslashes($new_instance['title']));
        $instance['hide_if_empty'] = !empty($new_instance['hide_if_empty']) ? 1 : 0;
        return $instance;
    }

    /** @see WP_Widget->form */
    function form($instance) {
        $hide_if_empty = isset($instance['hide_if_empty']) ? (bool) $instance['hide_if_empty'] : false;
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'woocommerce') ?></label>
            <input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php if (isset($instance['title'])) {
            echo esc_attr($instance['title']);
        } ?>" /></p>

        <p><input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id('hide_if_empty')); ?>" name="<?php echo esc_attr($this->get_field_name('hide_if_empty')); ?>"<?php checked($hide_if_empty); ?> />
            <label for="<?php echo $this->get_field_id('hide_if_empty'); ?>"><?php _e('Hide if cart is empty', 'woocommerce'); ?></label></p>
        <?php
    }

}

register_widget('AbeeCart');
?>